package frame;

import constant.Probabilities;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class DeveloperOptionsPanel extends JPanel {

    private MainFrame frame;
    private JSlider snakeInitLengthSlider; //调整蛇的初始长度的滑杆
    private JSlider snakeInitSpeedSlider; //调整蛇的初始速度的滑杆

    private JSlider normalFoodProbabilitySlider; //调整普通食物刷新概率的滑杆
    private JSlider speedUpFoodProbabilitySlider; //调整加速食物刷新概率的滑杆
    private JSlider shieldFoodProbabilitySlider; //调整护盾食物刷新概率的滑杆
    private JSlider throughWallFoodProbabilitySlider; //调整穿墙食物刷新概率的滑杆

    private int tempNormalFoodProbabilityValue;
    private int tempSpeedUpFoodProbabilityValue;
    private int tempSlowDownFoodProbabilityValue;
    private int tempShieldFoodProbabilityValue;
    private int tempThroughWallFoodProbabilityValue;



    public DeveloperOptionsPanel(MainFrame frame) {
        this.frame = frame;
        // 设置边界布局以便我们可以在左右两边添加填充
        this.setLayout(new BorderLayout());
        // 设置该面板的背景颜色
        this.setBackground(Color.white);

        /*可以直接更改set初始值  不知道我这里初始的设置和游戏实际情况是否符合*/
        frame.setSnakeInitLengthSlider(3);
        frame.setSnakeInitSpeedSlider(2);
        frame.setNormalFoodProbabilitySlider((int)(Probabilities.NORMAL_FOOD_PROB * 10));
        frame.setSpeedUpFoodProbabilitySlider((int)(Probabilities.SPEED_UP_FOOD_PROB * 10));
        frame.setShieldFoodProbabilitySlider((int)(Probabilities.SHIELD_FOOD_PROB * 10));
        frame.setThroughWallFoodProbabilitySlider((int)(Probabilities.THROUGH_WALL_FOOD_PROB * 10));
        int initLength = frame.getSnakeInitLengthSlider();
        int initSpeed = frame.getSnakeInitSpeedSlider();
        int normal = frame.getNormalFoodProbabilitySlider();
        int speedUp = frame.getSpeedUpFoodProbabilitySlider();
        int shield = frame.getShieldFoodProbabilitySlider();
        int throughWall = frame.getThroughWallFoodProbabilitySlider();

        /*北标题*/
        // 设置标题
        JLabel titleLabel = new JLabel("开发者界面");
        titleLabel.setFont(new Font("Courier", Font.BOLD, 40));
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        titleLabel.setOpaque(true);          // 标签设定为不透明
        titleLabel.setBackground(new Color(255, 255, 255));
        this.add(titleLabel, BorderLayout.NORTH);

        /*中间 slidersPanel*/
        //定义一个面板专门用来放置滑动条和标签，使用网格布局
        JPanel slidersPanel = new JPanel(new GridLayout(6,1));
        slidersPanel.setBackground(Color.white); // slidersPanel背景颜色

        //初始化
        snakeInitLengthSlider = createSlider(3, 10,initLength , 1, true, true);
        snakeInitSpeedSlider = createSlider(1, 10, initSpeed, 1, true, true);
        normalFoodProbabilitySlider = createSlider(0, 10, normal, 1, true, true);
        speedUpFoodProbabilitySlider = createSlider(0, 10, speedUp, 1, true, true);
        shieldFoodProbabilitySlider = createSlider(0, 10, shield, 1, true, true);
        throughWallFoodProbabilitySlider = createSlider(0, 10, throughWall, 1, true, true);

        // 添加组件到 slidersPanel
        addComponentsTo(slidersPanel, "蛇的初始长度：", snakeInitLengthSlider); // 蛇的初始状态滑杆
        addComponentsTo(slidersPanel, "蛇的初始速度：", snakeInitSpeedSlider);
        addComponentsTo(slidersPanel, "普通食物滑杆数值：", normalFoodProbabilitySlider);// 普通食物概率滑杆
        addComponentsTo(slidersPanel, "加速食物滑杆数值：", speedUpFoodProbabilitySlider);// 道具食物概率滑杆
        addComponentsTo(slidersPanel, "护盾食物滑杆数值：", shieldFoodProbabilitySlider);
        addComponentsTo(slidersPanel, "穿墙食物滑杆数值：", throughWallFoodProbabilitySlider);

        // 将slidersPanel添加到主面板的中央位置
        this.add(slidersPanel, BorderLayout.CENTER);

        /*南 button*/
        JPanel fillerSouth = new JPanel(new  GridLayout(3,1));
        fillerSouth.setOpaque(false);
        fillerSouth.setPreferredSize(new Dimension(0, 150)); //这里的50是左右间隔的宽度

        // 确认蛇的初始状态 button
        JButton  confirmInitSnakeStatus = new JButton("确定蛇的初始状态调整");
        confirmInitSnakeStatus.setPreferredSize(new Dimension(40, 50));
        confirmInitSnakeStatus.setBackground(new Color(72, 72, 72));//背景
        confirmInitSnakeStatus.setFont(new Font("Courier", Font.BOLD, 28));  // 设置字体、字形、字号
        confirmInitSnakeStatus.setForeground(new Color(255, 255, 255));  // 设置字体颜色
        confirmInitSnakeStatus.setOpaque(true);          // 标签设定为不透明
        confirmInitSnakeStatus.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 执行点击"确定蛇的初始状态调整"按钮后的操作
                JOptionPane.showMessageDialog(null, "蛇的的初始状态应用成功 !");
                setValue();

                // 输出当前蛇的状态到控制台
                System.out.println("-----------蛇的初始状态调整-----------");
                System.out.println("蛇的初始长度: " + snakeInitLengthSlider.getValue());
                System.out.println("蛇的初始速度: " + snakeInitSpeedSlider.getValue());
                System.out.println("普通食物概率: " + normalFoodProbabilitySlider.getValue());
            }
        });
        // 确认食物概率 button
        JButton confirmFoodProbabilityStatus = new JButton("确定道具食物概率调整 (食物概率总和应为10)");
        confirmFoodProbabilityStatus.setPreferredSize(new Dimension(40, 50));
        confirmFoodProbabilityStatus.setBackground(new Color(72, 72, 72));//背景
        confirmFoodProbabilityStatus.setFont(new Font("Courier", Font.BOLD, 28));  // 设置字体、字形、字号
        confirmFoodProbabilityStatus.setForeground(new Color(255, 255, 255));  // 设置字体颜色
        confirmFoodProbabilityStatus.setOpaque(true);          // 标签设定为不透明
        confirmFoodProbabilityStatus.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int total = speedUpFoodProbabilitySlider.getValue()
                        + shieldFoodProbabilitySlider.getValue()
                        + throughWallFoodProbabilitySlider.getValue();
                if (total != 10) {
                    JOptionPane.showMessageDialog(null, "道具食物概率总和为10，请重新调整！");
                    throughWallFoodProbabilitySlider.setValue(throughWallFoodProbabilitySlider.getValue() - (total - 10));
                } else {
                    JOptionPane.showMessageDialog(null, "道具食物概率设置应用成功 !");
                    setValue();

                    // 输出当前食物概率到控制台
                    System.out.println("-------------食物概率调整-------------");
                    System.out.println("加速食物刷新概率: " + speedUpFoodProbabilitySlider.getValue());
                    System.out.println("护盾食物刷新概率: " + shieldFoodProbabilitySlider.getValue());
                    System.out.println("穿墙食物刷新概率: " + throughWallFoodProbabilitySlider.getValue());
                }
            }
        });
        // 返回 button
        JButton backButton = new JButton("返回");
        backButton.setPreferredSize(new Dimension(40, 50));
        backButton.setBackground(new Color(255, 255, 255));//背景
        backButton.setFont(new Font("Courier", Font.BOLD, 28));  // 设置字体、字形、字号
        backButton.setForeground(new Color(72, 72, 72));  // 设置字体颜色
        backButton.setOpaque(true);          // 标签设定为不透明
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 跳转回到设置界面
                frame.setContentPane(new SettingPanel(frame));
                frame.invalidate();
                frame.validate();
                setValue();
            }
        });

        fillerSouth.add(confirmInitSnakeStatus);
        fillerSouth.add(confirmFoodProbabilityStatus);
        fillerSouth.add(backButton);
        this.add(fillerSouth,BorderLayout.SOUTH);

        /*东西空白 panel*/
        // 将它们的水平尺寸设为希望的间隔宽度，同时设置为透明以匹配背景颜色
        JPanel fillerWest = new JPanel();
        fillerWest.setOpaque(false);
        fillerWest.setPreferredSize(new Dimension(50, 500)); //这里的50是左右间隔的宽度

        JPanel fillerEast = new JPanel();
        fillerEast.setOpaque(false);
        fillerEast.setPreferredSize(new Dimension(50, 500));
        // 将填充面板添加到主面板的东西
        this.add(fillerWest, BorderLayout.WEST);
        this.add(fillerEast, BorderLayout.EAST);

        setValue();

    }

    private JSlider createSlider(int min, int max, int value, int majorTickSpacing, boolean paintTicks, boolean paintLabels) {
        //最小值 最大值  初始值 刻度
        JSlider slider = new JSlider(min, max, value);
        slider.setMajorTickSpacing(majorTickSpacing);
        slider.setPaintTicks(paintTicks);
        slider.setPaintLabels(paintLabels);
        //滑块字体
        slider.setFont(new Font("Courier", Font.BOLD, 25));  // 设置字体、字形、字号
        slider.setForeground(new Color(0, 0, 0));  // 设置字体颜色
        //滑块颜色
        slider.setBackground(Color.WHITE); // 设置滑杆背景颜色
        return slider;
    }

    private void addComponentsTo(JPanel panel, String label, JSlider slider) {
        // 创建一个标签来显示滑杆的当前值
        JLabel sliderLabel = new JLabel(label + slider.getValue());

        //滑杆标签字体
        sliderLabel.setFont(new Font("Courier", Font.BOLD, 30));  // 设置字体、字形、字号
        sliderLabel.setForeground(new Color(0, 0, 0));  // 设置字体颜色
        // 将标签添加到滑杆面板
        panel.add(sliderLabel);
        // 将滑杆添加到滑杆面板
        panel.add(slider);
        // 添加滑杆状态改变的监听器
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                //更新标签的文本以显示滑杆的当前值
                sliderLabel.setText(label + slider.getValue());
                // 在这里保存滑块的值
                switch (label) {
                    case "普通食物刷新概率：":
                        tempNormalFoodProbabilityValue = slider.getValue();
                        break;
                    case "加速食物刷新概率：":
                        tempSpeedUpFoodProbabilityValue = slider.getValue();
                        break;
                    case "护盾食物刷新概率：":
                        tempShieldFoodProbabilityValue = slider.getValue();
                        break;
                    case "穿墙食物刷新概率：":
                        tempThroughWallFoodProbabilityValue = slider.getValue();
                        break;
                }
            }
        });
    }

    public void setValue() {
        frame.setSnakeInitLengthSlider(snakeInitLengthSlider.getValue());//调整蛇的初始长度的滑杆
        frame.setSnakeInitSpeedSlider(snakeInitSpeedSlider.getValue()); //调整蛇的初始速度的滑杆
        frame.setNormalFoodProbabilitySlider(normalFoodProbabilitySlider.getValue());//跳转普通食物刷新概率的滑竿
        frame.setSpeedUpFoodProbabilitySlider(speedUpFoodProbabilitySlider.getValue());//调整加速食物刷新概率的滑杆
        frame.setShieldFoodProbabilitySlider(shieldFoodProbabilitySlider.getValue()); //调整护盾食物刷新概率的滑杆
        frame.setThroughWallFoodProbabilitySlider(throughWallFoodProbabilitySlider.getValue()); //调整穿墙食物刷新概率的滑杆
        frame.setIsDeveloper(true);
    }
}